package com.rtech.app.client;

import org.apache.log4j.Logger;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

public class RestClient {

	protected static Logger logger = Logger.getLogger("RestClient");
	private static final String REST_SERVICE_URL_DIR = "http://localhost:8080/file-system-rest-service/dirSearch?dirPath={0}";
	private static final String REST_SERVICE_URL_FILE = "http://localhost:8080/file-system-rest-service/fileSearch?filePath={0}";

	private RestTemplate restTemplate;

	/**
	 * Method to get Server direction information via RESTFUL Service.
	 * 
	 */
	public String directorySearch(String dirPath) {

		return search(REST_SERVICE_URL_DIR, dirPath);
	}

	public String fileSearch(String filepath) {

		return search(REST_SERVICE_URL_FILE, filepath);
	}

	public String search(String URL, String path) {

		ResponseEntity<String> responseEntity = null;
		String body = null;
		try {
			responseEntity = restTemplate.getForEntity(URL, String.class, path);
			body = responseEntity.getBody();
		} catch (ServiceException e) {
			logger.info("Service exception");
			body = e.getMessage();
		} catch (Exception e) {
			logger.info("Fatal exception");
			body = e.getMessage();
		}
		return body;
	}

	public RestTemplate getRestTemplate() {
		return restTemplate;
	}

	public void setRestTemplate(RestTemplate restTemplate) {
		this.restTemplate = restTemplate;
	}

}
